package org.example.device.control;

import org.example.device.protocol.DeviceProtocol;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 智能灯光控制器
 * 实现智能灯光的控制逻辑
 */
@Component
public class SmartLightController extends AbstractDeviceController {
    
    private static final String DEVICE_TYPE = "SMART_LIGHT";
    private static final List<String> SUPPORTED_COMMANDS = Arrays.asList(
        "switch", "brightness", "color", "color_temperature", "mode"
    );
    
    @Override
    public Map<String, Object> getDeviceProperties(Long deviceId) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("deviceType", DEVICE_TYPE);
        properties.put("supportedCommands", SUPPORTED_COMMANDS);
        properties.put("maxBrightness", 100);
        properties.put("minBrightness", 1);
        properties.put("supportedColors", Arrays.asList("red", "green", "blue", "white", "yellow", "purple"));
        properties.put("colorModes", Arrays.asList("rgb", "white", "colorful"));
        return properties;
    }
    
    @Override
    public DeviceControlResult setDeviceProperties(Long deviceId, Map<String, Object> properties) {
        try {
            // 验证属性
            if (!validateLightProperties(properties)) {
                return DeviceControlResult.failure("灯光属性验证失败", "INVALID_PROPERTIES");
            }
            
            // 构建控制命令
            DeviceControlCommand command = new DeviceControlCommand(deviceId, "properties", properties);
            return executeCommand(command);
        } catch (Exception e) {
            return DeviceControlResult.failure("设置灯光属性失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<String> getSupportedCommands(Long deviceId) {
        return new ArrayList<>(SUPPORTED_COMMANDS);
    }
    
    @Override
    public String getDeviceType() {
        return DEVICE_TYPE;
    }
    
    /**
     * 智能灯光使用MQTT协议
     */
    @Override
    protected DeviceProtocol getDeviceProtocol() {
        return protocolRegistry.getProtocol("MQTT");
    }
    
    /**
     * 验证灯光属性
     */
    private boolean validateLightProperties(Map<String, Object> properties) {
        if (properties == null) return false;
        
        // 验证亮度
        if (properties.containsKey("brightness")) {
            Object brightness = properties.get("brightness");
            if (brightness instanceof Number) {
                int value = ((Number) brightness).intValue();
                if (value < 1 || value > 100) {
                    return false;
                }
            }
        }
        
        // 验证颜色
        if (properties.containsKey("color")) {
            Object color = properties.get("color");
            if (!(color instanceof String)) {
                return false;
            }
        }
        
        // 验证色温
        if (properties.containsKey("colorTemperature")) {
            Object temp = properties.get("colorTemperature");
            if (temp instanceof Number) {
                int value = ((Number) temp).intValue();
                if (value < 2700 || value > 6500) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    /**
     * 控制灯光开关
     */
    public DeviceControlResult controlSwitch(Long deviceId, boolean on, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "switch", on);
        command.setUserId(userId);
        command.setAction("灯光开关控制");
        return executeCommand(command);
    }
    
    /**
     * 控制灯光亮度
     */
    public DeviceControlResult controlBrightness(Long deviceId, int brightness, Long userId) {
        if (brightness < 1 || brightness > 100) {
            return DeviceControlResult.failure("亮度值必须在1-100之间", "INVALID_BRIGHTNESS");
        }
        
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "brightness", brightness);
        command.setUserId(userId);
        command.setAction("灯光亮度控制");
        return executeCommand(command);
    }
    
    /**
     * 控制灯光颜色
     */
    public DeviceControlResult controlColor(Long deviceId, String color, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "color", color);
        command.setUserId(userId);
        command.setAction("灯光颜色控制");
        return executeCommand(command);
    }
    
    /**
     * 控制灯光模式
     */
    public DeviceControlResult controlMode(Long deviceId, String mode, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "mode", mode);
        command.setUserId(userId);
        command.setAction("灯光模式控制");
        return executeCommand(command);
    }
}
